﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex1.BufferedConsole
{
    class BufferedConsole : IDisposable
    {
        private StringBuilder builder;
        private int size;

        public BufferedConsole()
        {
            builder = new StringBuilder();
            size = 25;
        }
        
        ~BufferedConsole()
        {
            Console.WriteLine("~BufferedConsole() called!");
            Dispose(false);
        }
        
        public void Dispose()
        {
            Console.WriteLine("Dispose() called!");
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                Console.WriteLine(builder);
            }
        }

        public void Write(string str)
        {
            foreach (var ch in str)
            {
                builder.Append(ch);
                if (builder.Length == size)
                {
                    Console.WriteLine(builder);
                    builder.Clear();
                }
            }
        }
    }
}
